package j02zuoye;

import java.util.Random;

/*
验证码生成
在StringTest2类里编写一个 public static String randomString(int length) 方法, length是字符串的长度,randomString方法的作用是随机产生一个该长度的字符串,字符串里只能包含大写,小写字母以及数字. 在main方法里测试randomString方法的功能.
// s = "abc-zA-Z0-9"
5
aaabb

 */
public class Z4 {
    public static void main(String[] args) {
        System.out.println("randomString(5) = " + randomString(5));
        System.out.println("randomString(3) = " + randomString(3));
        System.out.println("randomString(7) = " + randomString(7));
        System.out.println("randomString(1) = " + randomString(1));
        System.out.println("randomString(5) = " + randomString(5));
    }
    public static String randomString(int length){
        //数据字典
        String dicStr = "";
        //0-9
        for (int i = 0; i < 10; i++) {
            dicStr += i;
        }
        //A-Z
        for (int i = 'A'; i <= 'Z'; i++) {
            dicStr+=(char)i;
        }
        //a-z
        for (int i = 'a'; i <= 'z'; i++) {
            dicStr+=(char)i;
        }
//        System.out.println("dicStr = " + dicStr);
        String r = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            //随机下标
            int pos = random.nextInt(dicStr.length());
            r += dicStr.charAt(pos);
        }
        return r;
    }
}
